import Vue from "vue";
import Vuex from "vuex";
import createPersistedState from "vuex-persistedstate";

Vue.use(Vuex);
export default new Vuex.Store({
  state: {
    token: '',  // token
    power: [],  // 权限
    cancel: [], // 路由跳转取消请求
    userinfo: {}  // 登录信息
  },
  getters: {
    token: (state) => state.token,
    power: (state) => state.power,
    cancel: (state) => state.cancel,
    userinfo: (state) => state.userinfo,
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_POWER: (state, power) => {
      state.power = power;
    },
    SET_USERINFO: (state, userinfo) => {
      state.userinfo = userinfo;
    },
    PUSH_CANCEL(state, payload) {
      let data = state.cancel;
      if (payload && payload.cancelToken) {
        data.push(payload.cancelToken);
      }
      state.cancel = data.filter((item) => {
        return Boolean(item);
      });
    },
    CLAER_CANCEL({ cancel }) {
      cancel?.forEach((item) => {
        if (item) {
          item("路由跳转取消请求");
        }
      });
      cancel = [];
    },
  },
  actions: {},
  modules: {},
  plugins: [
    createPersistedState({
      storage: window.localStorage,
    }),
  ],
});
